// pages/receiving/receiving.js
import dataParser from  '../../utils/dataParser';
import backPareser from '../../utils/backPareser'
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
   orders:[
    {
      "id": 1,
      "orderid": "ORD202503062037041311uhxo",
      "servicename": "厨房清洁服务",
      "serviceprice": 150,
      "picurl": "http://192.168.101.65:9000/photos/1/servicepic/4d88c4838b42968a93b643c015a031f1.png",
      "serviceid": 28,
      "acceptid": null,
      "acceptname": null,
      "createat": "2025-03-06T12:36:19.000+00:00",
      "accepttel": null,
      "status": 0,
      "specification": "厨/房/清",
      "address": "天津市南开区卫津路94号",
      "updateat": "2025-03-06T12:37:04.000+00:00",
      "startservicetime": "2025-03-05T04:00:00.000+00:00",
      "endservicetime": "2025-03-05T05:00:00.000+00:00"
    }
   ],
  orderMes:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  getAvailableOrder(){

  },
  onLoad(options) {
    this.getAll();
    const parseOreders=dataParser.parseSericeSpecifications(this.data.orders)
    this.setData({
      orders:parseOreders
    })
    console.log(this.data.orders);



  },
 

  getAll(){
    wx.request({
      url: 'http://localhost:9001/wx/getAvailableOrder',
      method:'GET',
      withCredentials: true, 
      success:(res)=>{
        this.setData({
          orders:res.data.data
        })
        const parseOreders=dataParser.parseSericeSpecifications(this.data.orders)
        this.setData({
          orders:parseOreders
        })
      }
    })
    
  },

  pickUpOrder(event){

   wx.getStorage({
    key:'sessionId',
    success:(res1)=>{
      const cookie = `JSESSIONID=${res1.data}`; // 确保 cookie 格式正确
      this.setData({ cookie });

this.setData({
  orderMes:event.currentTarget.dataset.index
})
    
      const spec=this.data.orderMes.specification.join('/')
      console.log(spec);
   
   
     console.log(111);
     console.log(this.data.orderMes);
      wx.request({
        url: 'http://localhost:9001/wx/pickUpOrder',
        method:'POST',
        withCredentials: true, 
        header: {
          'cookie': cookie
        },
        data:{
         "id": this.data.orderMes.id,
         "orderid": this.data.orderMes.orderid,
         "servicename": this.data.orderMes.servicename,
         "price": this.data.orderMes.price,
         "picurl": this.data.orderMes.picurl,
         "serviceid": this.data.orderMes.serviceid,
         "acceptid": this.data.orderMes.acceptid,
         "acceptname": this.data.orderMes.acceptname,
         "createat": this.data.orderMes.createat,
         "accepttel": this.data.orderMes.accepttel,
         "status": this.data.orderMes.status,
         "specification": spec,
         "address": this.data.orderMes.address,
         "updateat": this.data.orderMes.updateat,
         "startservicetime":this.data.orderMes.startservicetime ,
         "endservicetime": this.data.orderMes.endservicetime 
        },
        success(res) {
         console.log('订单提交成功:', res);
         Toast.success('接单成功');
         this.getAll()
       },
       fail(err) {
         console.error('订单提交失败:', err);
       }
 
      })

    }
   })

  },

   MyPickOrder(){
    wx.navigateTo({
      url: '/pages/pickeOrder/pickeOrder'
    });
   

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})